﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Eqwww.Domain.Models.SystemManage;
using Eqwww.Code;
using System.Text;

namespace Eqwww.TagHelpers
{
    public class TreeCollapseToggleTagHelper : Microsoft.AspNetCore.Razor.TagHelpers.TagHelper
    {
        public List<TreeCollapseToggleModel> Items { get; set; }

        public bool? DisableAllToggle { get; set; }

        private int layer = 0;

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            output.TagName = "ul";
            var sb = GetCollapse(Items);
            output.Content.SetHtmlContent(sb);

        }

        private string GetCollapse(List<TreeCollapseToggleModel> data)
        {
            var sb = new StringBuilder();
            sb.Append(GetChildren(data, null));
            return sb.ToString();
        }

        public string GetChildren(List<TreeCollapseToggleModel> data, string parentId)
        {
            var sb = new StringBuilder();
            var children = data.FindAll(x => x.ParentId == parentId).OrderBy(x => x.SortCode);
            foreach (var item in children)
            {
                if (data.Any(x=>x.ParentId==item.Id))
                {
                    sb.Append("<li class=\"mdui-collapse-item  " + (item.Expend == true ? " mdui-collapse-item-open" : string.Empty) + " \">");
                    sb.Append("<div class=\"mdui-collapse-item-header mdui-list-item mdui-ripple\">");

                    if (item.CanToggle == true)
                    {
                        sb.Append("<label class=\"mdui-switch\">");
                        sb.Append("<input name=\"permission\" id=\"" + item.Id + "\" value=\"" + item.Id + "\" type=\"checkbox\" " + (item.ToggleMark == true ? "checked" : string.Empty) + "  " + ((DisableAllToggle == true || item.Disabled == true) ? "disabled='disabled'" : string.Empty) + " />");
                        sb.Append("<i class=\"mdui-switch-icon\"></i>");
                        sb.Append("</label>");
                    }
                    if (!string.IsNullOrEmpty(item.Icon))
                    {
                        sb.Append("<div class=\"mdui-p-l-2\"></div><i class=\"mdui-collapse-item-arrow mdui-icon mdui-text-color-grey material-icons\">"+item.Icon+"</i>");
                    }
                    if (string.IsNullOrEmpty(item.Url))
                    {
                        sb.Append("<div class=\"mdui-list-item-content\">" + item.Text + "</div>");
                    }
                    else
                    {
                        sb.Append("<div class=\"mdui-list-item-content\">");

                        sb.Append("<div class=\"mdui-list-item-title mdui-list-item-one-line\">" + item.Text + "</div>");
                        sb.Append("<div class=\"mdui-list-item-text mdui-list-item-two-line\" style=\"height:auto;\">"+item.Url+"</div>");

                        sb.Append("</div>");
                    }
                    

                    sb.Append("<i class=\"mdui-collapse-item-arrow mdui-icon material-icons\">keyboard_arrow_down</i>");

                    sb.Append("</div>");

                    var needMargin = false;
                    var parent = data.FirstOrDefault(x => x.Id == item.ParentId);
                    if (parent != null)
                    {
                        
                            needMargin = true;
                        
                    }
                    sb.Append("<ul class=\"mdui-collapse-item-body mdui-list\" "+(needMargin?"style='padding-left:40px;'":string.Empty)+" mdui-collapse>");

                    sb.Append(GetChildren(data, item.Id));

                    sb.Append("</ul>");
                    
                    sb.Append("</li>");
                    sb.Append("<li class='mdui-divider-inset mdui-m-y-0'></li>");
                }
                else
                {
                    sb.Append("<li class=\"mdui-list-item mdui-ripple\">");
                    if (item.CanToggle==true)
                    {
                        sb.Append("<label class=\"mdui-switch\">");
                        sb.Append("<input name=\"permission\" id=\"" + item.Id + "\" value=\"" + item.Id + "\" type=\"checkbox\" " + (item.ToggleMark==true ? "checked" : string.Empty) + " " + ((DisableAllToggle==true|| item.Disabled == true) ? "disabled='disabled'" : string.Empty) + " />");
                        sb.Append("<i class=\"mdui-switch-icon\"></i>");
                        sb.Append("</label>");
                    }
                    if (!string.IsNullOrEmpty(item.Icon))
                    {
                        sb.Append("<div class=\"mdui-p-l-2\"></div><i class=\"mdui-collapse-item-arrow mdui-icon mdui-text-color-grey material-icons\">" + item.Icon + "</i>");
                    }
                    if (string.IsNullOrEmpty(item.Url))
                    {
                        sb.Append("<div class=\"mdui-list-item-content\">" + item.Text + "</div>");
                    }
                    else
                    {
                        sb.Append("<div class=\"mdui-list-item-content\">");

                        sb.Append("<div class=\"mdui-list-item-title mdui-list-item-one-line\">" + item.Text + "</div>");
                        sb.Append("<div class=\"mdui-list-item-text mdui-list-item-two-line\" style=\"height:auto;\">" + item.Url + "</div>");

                        sb.Append("</div>");
                    }

                    sb.Append("</li>");

                }

                
            }
            layer++;

            return sb.ToString();
        }


    }
}
